package com.sun.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.entity.Msg;
import com.sun.entity.Store;
import com.sun.entity.User;
import com.sun.service.MsgService;
import com.sun.service.StoreService;
import com.sun.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/3/30 14:03
 * @FileName: ExpiryPeriodWarning
 * @Description: ""
 */
public class ExpiryPeriodWarning extends QuartzJobBean {
    @Autowired
    private StoreService storeService;

    @Autowired
    private UserService userService;
    @Autowired
    private MsgService msgService;

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //TODO 这里写定时任务的执行逻辑
        //查询出药品过期时间小于1个月的药品
        Map<String, String> list1 = new HashMap<>();
        Map<String, String> list2 = new HashMap<>();

        LambdaQueryWrapper<Store> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(Store::getDrugNumber, 0);
        List<Store> storeList = storeService.list(lambdaQueryWrapper);
        LocalDateTime now = LocalDateTime.now();
        for (Store store : storeList) {
            LocalDateTime usefulDate = store.getUsefulDate();
            boolean flag = now.isBefore(usefulDate);
            if (flag) {
                Duration duration = Duration.between(now, usefulDate);
                long days = duration.toDays();
                if (days < 30) {
                    String str = String.format("%s,%s日,%s距离过期还有%s日,它的库存还有%s,优先处理这些药品", store.getAddr(), now.toString().substring(0, 10), store.getDrugName(), days, store.getDrugNumber());
                    list1.put(store.getDrugName(), str);
                }
            } else {
                String str = String.format("%s,%s日,%s药品已过期,它的库存还有%s,请处理这些药品", store.getAddr(), now.toString().substring(0, 10), store.getDrugName(), store.getDrugNumber());
                list2.put(store.getDrugName(), str);
            }

        }
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getType, 2);
        userLambdaQueryWrapper.eq(User::getStatus, 1);
        List<User> users = userService.list(userLambdaQueryWrapper);
        sendMsg(list1, now, "有效期预警", users);
        sendMsg(list2, now, "过期药品", users);
    }

    public void sendMsg(Map<String, String> list, LocalDateTime now, String type, List<User> users) {
        if (!list.isEmpty()) {
            for (Map.Entry<String, String> item : list.entrySet()) {
                for (User user : users) {
                    Msg msg = new Msg();
                    msg.setDrugName(item.getKey());
                    msg.setTitle(type);
                    msg.setContent(item.getValue());
                    msg.setDateTime(now);
                    msg.setStatus(0);
                    msg.setMsgUserId(1L);
                    msg.setUserId(user.getId());
                    msgService.save(msg);
                }
            }
        }
    }
}
